package com.ssy.lingxi.contract.esign.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.constant.RequestMethod;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import com.ssy.lingxi.contract.esign.request.*;
import com.ssy.lingxi.contract.esign.response.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 签署方账户API
 * @author yzc
 * @version 2.0.0
 * @date 2020/8/26
 */
public class Account {

    private final AuthToken authToken;
    private final EsignProperties properties;

    public Account(AuthToken authToken, EsignProperties properties) {
        this.authToken = authToken;
        this.properties = properties;
    }

    /**
     * 个人账号创建
     */
    public PersonalAccountCreateResponse personalAccountCreate(PersonalAccountCreateRequest request) throws EsignException {
        String resourcePath = "/v1/accounts/createByThirdPartyUserId";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, PersonalAccountCreateResponse.class);
    }

    /**
     * 个人账户修改(按照账号ID修改)
     */
    public PersonalAccountUpdateResponse personalAccountUpdate(PersonalAccountUpdateRequest request) throws EsignException {
        String resourcePath = "/v1/accounts/{accountId}";
        RequestMethod method = RequestMethod.PUT;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, PersonalAccountUpdateResponse.class);
    }

    /**
     * 个人账户修改(按照第三方用户ID修改)
     */
    public PersonalAccountUpdateResponse personalAccountThirdIdUpdate(PersonalAccountThirdIdUpdate request) throws EsignException {
        String resourcePath = "/v1/accounts/updateByThirdId";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, PersonalAccountUpdateResponse.class);
    }

    /**
     * 查询个人账户（按照账户ID查询）
     */
    public PersonalAccountDetailResponse personalAccountDetail(String accountId) throws EsignException {
        String resourcePath = "/v1/accounts/{accountId}";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("accountId", accountId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, PersonalAccountDetailResponse.class);
    }

    /**
     * 查询个人账户（按照第三方用户ID查询）
     */
    public PersonalAccountDetailResponse personalAccountByThirdId(String thirdPartyUserId) throws EsignException {
        String resourcePath = "/v1/accounts/getByThirdId";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("thirdPartyUserId", thirdPartyUserId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, PersonalAccountDetailResponse.class);
    }

    /**
     * 注销个人账户（按照账号ID注销）
     */
    public void personalAccountDelete(String accountId) throws EsignException {
        String resourcePath = "/v1/accounts/{accountId}";
        RequestMethod method = RequestMethod.DELETE;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("accountId", accountId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

    /**
     * 机构账号创建
     */
    public OrgAccountCreateResponse orgAccountCreate(OrgAccountCreateRequest request) throws EsignException {
        String resourcePath = "/v1/organizations/createByThirdPartyUserId";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, OrgAccountCreateResponse.class);
    }

    /**
     * 机构账号修改（按照账号ID修改）
     */
    public OrgAccountUpdateResponse orgAccountUpdate(OrgAccountUpdateRequest request) throws EsignException {
        String resourcePath = "/v1/organizations/{orgId}";
        RequestMethod method = RequestMethod.PUT;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, OrgAccountUpdateResponse.class);
    }

    /**
     * 机构账号修改（按照第三方机构ID修改）
     */
    public OrgAccountUpdateResponse orgAccountThirdIdUpdate(OrgAccountThirdIdUpdateRequest request) throws EsignException {
        String resourcePath = "/v1/organizations/updateByThirdId";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, OrgAccountUpdateResponse.class);
    }

    /**
     * 查询机构账号（按照账号ID查询）
     */
    public OrgAccountDetailResponse orgAccountDetail(String orgId) throws EsignException {
        String resourcePath = "/v1/organizations/{orgId}";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orgId", orgId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, OrgAccountDetailResponse.class);
    }

    /**
     * 查询机构账号（按照第三方机构ID查询）
     */
    public OrgAccountDetailResponse orgAccountByThirdId(String thirdPartyUserId) throws EsignException {
        String resourcePath = "/v1/organizations/getByThirdId";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("thirdPartyUserId", thirdPartyUserId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, OrgAccountDetailResponse.class);
    }

    /**
     * 注销机构账号（按照账号ID注销）
     */
    public void orgAccountDelete(String orgId) throws EsignException {
        String resourcePath = "/v1/organizations/{orgId}";
        RequestMethod method = RequestMethod.DELETE;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orgId", orgId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        EsignResource.execute(esignRequest);
    }

    /**
     * 查询认证主流程明细
     * @param flowId 认证流程id
     */
    public AuthFlowDetailResponse authFlowDetail(String flowId) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/common/{flowId}/outline";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("flowId", flowId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, AuthFlowDetailResponse.class);
    }

    /**
     * 设置静默签署
     */
    public void silentSignSet(SilentSignSetRequest request) throws EsignException {
        String resourcePath = "/v1/signAuth/{accountId}";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

    /**
     * 撤销静默签署
     * @param accountId 授权人id，即个人账号id或机构账号id
     */
    public void silentSignCancel(String accountId) throws EsignException {
        String resourcePath = "/v1/signAuth/{accountId}";
        RequestMethod method = RequestMethod.DELETE;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("accountId", accountId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }
}
